package com.example.rpc_core;

import cn.hutool.core.util.IdUtil;
import com.example.rpc_core.constance.ProtocolConstant;
import com.example.rpc_core.constance.RpcConstant;
import com.example.rpc_core.model.RpcRequestExample;
import com.example.rpc_core.protocal.*;
import com.example.rpc_core.protocal.ProtocolMessageTypeEnum;
import io.vertx.core.buffer.Buffer;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class test1 {

        @Test
        public void testEncodeAndDecode() throws IOException {
            // 构造消息
            ProtocolMessage<RpcRequestExample> protocolMessage = new ProtocolMessage<>();
            ProtocolMessage.Header header = new ProtocolMessage.Header();
            header.setMagic(ProtocolConstant.PROTOCOL_MAGIC);
            header.setVersion(ProtocolConstant.PROTOCOL_VERSION);
            header.setSerializable((byte) ProtocolMessageSerializableEnum.JDK.getKey());
            header.setType((byte) ProtocolMessageTypeEnum.REQUEST.getKey());
            header.setStatus((byte) ProtocolStatusCode.OK.getStatus());
            header.setRequestId(IdUtil.getSnowflakeNextId());
            header.setBodyLength(0);
            RpcRequestExample rpcRequest = new RpcRequestExample();
            rpcRequest.setServiceName("myService");
            rpcRequest.setMethodName("myMethod");
            rpcRequest.setServiceVersion(RpcConstant.DEFAULT_CONFIG_Version);
            rpcRequest.setParameterTypes(new Class[]{String.class});
            rpcRequest.setArgs(new Object[]{"aaa", "bbb"});
            protocolMessage.setHeader(header);
            protocolMessage.setBody(rpcRequest);

            Buffer encodeBuffer = ProtocolMessageEnder.encode(protocolMessage);
            ProtocolMessage<?> message = ProtocolMessageDecoder.decode(encodeBuffer);
            Assert.assertNotNull(message);
        }

    }

